﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using Dictionary.ERP;
using Common;
using Extra.ERP;
using Common.Class;
using Security.ERP;

namespace Dictionary
{
    public class xucSymbolAdd : xucBaseAdd
    {
        public delegate void SuccessEventHander(object sender, DIC_SYMBOL item);

        private readonly IContainer components = null;

        private CalcEdit calPercentSalary;
        private CheckEdit cheIsEdit;
        private CheckEdit cheIsShow;
        private LabelControl labelControl1;
        private LabelControl labelControl2;
        private LabelControl labelControl3;
        private TextEdit txtDescription;

        public xucSymbolAdd()
        {
            InitializeComponent();
        }

        public event SuccessEventHander Success;

        protected override void Add()
        {
            base.Add();
            base.txtID.Text = new DIC_SYMBOL().NewID();
            base.txtNAME.Focus();
        }

        public void Clear()
        {
            base.txtID.Text = "";
            base.txtNAME.Text = "";
            base.txtNAME.Properties.ReadOnly = false;
            txtDescription.Text = "";
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        protected override void Init()
        {
        }

        private void InitializeComponent()
        {
            var manager = new ComponentResourceManager(typeof (xucSymbolAdd));
            txtDescription = new TextEdit();
            cheIsEdit = new CheckEdit();
            calPercentSalary = new CalcEdit();
            labelControl1 = new LabelControl();
            labelControl2 = new LabelControl();
            labelControl3 = new LabelControl();
            cheIsShow = new CheckEdit();
            base.gcInfo.BeginInit();
            base.gcInfo.SuspendLayout();
            base.txtNAME.Properties.BeginInit();
            base.txtID.Properties.BeginInit();
            base.imageCollection2.BeginInit();
            ((ISupportInitialize) base.Err).BeginInit();
            txtDescription.Properties.BeginInit();
            cheIsEdit.Properties.BeginInit();
            calPercentSalary.Properties.BeginInit();
            cheIsShow.Properties.BeginInit();
            base.SuspendLayout();
            base.gcInfo.Controls.Add(labelControl3);
            base.gcInfo.Controls.Add(labelControl2);
            base.gcInfo.Controls.Add(txtDescription);
            base.gcInfo.Controls.Add(labelControl1);
            base.gcInfo.Controls.Add(cheIsShow);
            base.gcInfo.Controls.Add(cheIsEdit);
            base.gcInfo.Controls.Add(calPercentSalary);
            base.gcInfo.Size = new Size(0x15c, 0xc2);
            base.gcInfo.Controls.SetChildIndex(calPercentSalary, 0);
            base.gcInfo.Controls.SetChildIndex(base.lblId, 0);
            base.gcInfo.Controls.SetChildIndex(base.lblName, 0);
            base.gcInfo.Controls.SetChildIndex(cheIsEdit, 0);
            base.gcInfo.Controls.SetChildIndex(cheIsShow, 0);
            base.gcInfo.Controls.SetChildIndex(base.txtID, 0);
            base.gcInfo.Controls.SetChildIndex(labelControl1, 0);
            base.gcInfo.Controls.SetChildIndex(txtDescription, 0);
            base.gcInfo.Controls.SetChildIndex(base.txtNAME, 0);
            base.gcInfo.Controls.SetChildIndex(labelControl2, 0);
            base.gcInfo.Controls.SetChildIndex(labelControl3, 0);
            base.txtNAME.Location = new Point(0x57, 0x34);
            base.txtNAME.Size = new Size(0xf8, 20);
            base.lblName.Location = new Point(11, 0x37);
            base.lblName.Size = new Size(0x3b, 13);
            base.lblName.Text = "T\x00ean k\x00fd hiệu:";
            base.txtID.Location = new Point(0x57, 0x1a);
            base.txtID.Size = new Size(0xf8, 20);
            base.lblId.Location = new Point(10, 0x1d);
            base.imageCollection2.ImageStream =
                (ImageCollectionStreamer) manager.GetObject("imageCollection2.ImageStream");
            base.btnSave.Location = new Point(0x2e, 210);
            base.btnCancel.Location = new Point(0xed, 210);
            base.btnSaveNew.Location = new Point(0x90, 210);
            txtDescription.Location = new Point(0x57, 110);
            txtDescription.Name = "txtDescription";
            txtDescription.Size = new Size(0xf8, 20);
            txtDescription.TabIndex = 4;
            cheIsEdit.EditValue = true;
            cheIsEdit.Location = new Point(0x55, 0x8b);
            cheIsEdit.Name = "cheIsEdit";
            cheIsEdit.Properties.Caption = "Được ph\x00e9p chỉnh sửa";
            cheIsEdit.Size = new Size(0xc5, 0x13);
            cheIsEdit.TabIndex = 5;
            var bits = new int[4];
            calPercentSalary.EditValue = new decimal(bits);
            calPercentSalary.Location = new Point(0x57, 0x51);
            calPercentSalary.Name = "calPercentSalary";
            calPercentSalary.Properties.Buttons.AddRange(new[] {new EditorButton(ButtonPredefines.Combo)});
            calPercentSalary.Size = new Size(0x6f, 20);
            calPercentSalary.TabIndex = 6;
            labelControl1.Location = new Point(11, 0x54);
            labelControl1.Name = "labelControl1";
            labelControl1.Size = new Size(0x33, 13);
            labelControl1.TabIndex = 7;
            labelControl1.Text = "Trừ lương:";
            labelControl2.Location = new Point(12, 0x71);
            labelControl2.Name = "labelControl2";
            labelControl2.Size = new Size(0x27, 13);
            labelControl2.TabIndex = 8;
            labelControl2.Text = "Ghi ch\x00fa:";
            labelControl3.Location = new Point(0xcc, 0x54);
            labelControl3.Name = "labelControl3";
            labelControl3.Size = new Size(0x5e, 13);
            labelControl3.TabIndex = 9;
            labelControl3.Text = "% x Lương căn bản";
            cheIsShow.EditValue = true;
            cheIsShow.Location = new Point(0x55, 0xa4);
            cheIsShow.Name = "cheIsShow";
            cheIsShow.Properties.Caption = "Hiển thị k\x00fd hiệu chấm c\x00f4ng n\x00e0y";
            cheIsShow.Size = new Size(0xc5, 0x13);
            cheIsShow.TabIndex = 5;
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.Name = "xucSymbolAdd";
            base.Size = new Size(350, 250);
            base.Load += new EventHandler(xucSymbolAdd_Load);
            base.gcInfo.EndInit();
            base.gcInfo.ResumeLayout(false);
            base.gcInfo.PerformLayout();
            base.txtNAME.Properties.EndInit();
            base.txtID.Properties.EndInit();
            base.imageCollection2.EndInit();
            ((ISupportInitialize) base.Err).EndInit();
            txtDescription.Properties.EndInit();
            cheIsEdit.Properties.EndInit();
            calPercentSalary.Properties.EndInit();
            cheIsShow.Properties.EndInit();
            base.ResumeLayout(false);
        }

        private void RaiseSuccessEventHander(DIC_SYMBOL item)
        {
            if (Success != null)
            {
                Success(this, item);
            }
        }

        public void SetData(DIC_SYMBOL item)
        {
            base.txtID.Text = item.SymbolCode;
            SYS_LOG.Insert("Danh Mục K\x00fd Hiệu Chấm C\x00f4ng", "Xem", base.txtID.Text);
            if (base.m_Status == Actions.Update)
            {
                base.txtID.Properties.ReadOnly = true;
            }
            if (!item.IsEdit)
            {
                base.txtNAME.Properties.ReadOnly = true;
            }
            base.txtNAME.Text = item.SymbolName;
            calPercentSalary.Text = item.PercentSalary.ToString();
            cheIsEdit.Checked = item.IsEdit;
            cheIsShow.Checked = item.IsShow;
            txtDescription.Text = item.Description;
        }

        protected override void txtID_EditValueChanged(object sender, EventArgs e)
        {
            var control = (TextEdit) sender;
            if (control.ErrorText != string.Empty)
            {
                base.Err.SetError(control, string.Empty);
            }
            if (base.m_Status == Actions.Add)
            {
                var dic_symbol = new DIC_SYMBOL();
                if (dic_symbol.Exist(control.Text))
                {
                    base.Err.SetError(control, "M\x00e3 đ\x00e3 tồn tại.");
                    control.Focus();
                }
            }
        }

        protected override void txtID_KeyDown(object sender, KeyEventArgs e)
        {
            var control = (TextEdit) sender;
            if ((e.KeyCode == Keys.Enter) | (e.KeyCode == Keys.Tab))
            {
                var dic_symbol = new DIC_SYMBOL();
                if ((base.m_Status == Actions.Add) && dic_symbol.Exist(control.Text))
                {
                    base.Err.SetError(control, "M\x00e3 đ\x00e3 tồn tại.");
                    control.Focus();
                }
            }
        }

        protected override string uc_Change()
        {
            return string.Empty;
        }

        protected override string uc_Delete()
        {
            var item = new DIC_SYMBOL
            {
                SymbolCode = base.txtID.Text
            };
            string str = item.Delete();
            if (str == "OK")
            {
                RaiseSuccessEventHander(item);
            }
            return str;
        }

        protected override string uc_Save()
        {
            if (MyRule.Get(MyLogin.RoleId, "bbiSymbol") != "OK")
            {
                return "";
            }
            if (!MyRule.AllowAdd)
            {
                MyRule.Notify();
                return "";
            }
            SYS_LOG.Insert("Danh Mục K\x00fd Hiệu Chấm C\x00f4ng", "Th\x00eam", base.txtID.Text);
            base.SetWaitDialogCaption("Đang lưu dữ liệu...");
            Cursor.Current = Cursors.WaitCursor;
            var item = new DIC_SYMBOL(base.txtID.Text, base.txtNAME.Text, double.Parse(calPercentSalary.Text),
                cheIsEdit.Checked, cheIsShow.Checked, txtDescription.Text);
            string text = item.Insert();
            if (text == "OK")
            {
                RaiseSuccessEventHander(item);
            }
            Cursor.Current = Cursors.Default;
            this.DoHide();
            if (text != "OK")
            {
                XtraMessageBox.Show(text, "Cảnh B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            return text;
        }

        protected override string uc_Update()
        {
            if (MyRule.Get(MyLogin.RoleId, "bbiSymbol") != "OK")
            {
                return "";
            }
            if (!MyRule.AllowEdit)
            {
                MyRule.Notify();
                return "";
            }
            SYS_LOG.Insert("Danh Mục K\x00fd Hiệu Chấm C\x00f4ng", "Cập Nhật", base.txtID.Text);
            base.SetWaitDialogCaption("Đang cập nhật dữ liệu...");
            var item = new DIC_SYMBOL(base.txtID.Text, base.txtNAME.Text, double.Parse(calPercentSalary.Text),
                cheIsEdit.Checked, cheIsShow.Checked, txtDescription.Text);
            string text = item.Update();
            if (text == "OK")
            {
                RaiseSuccessEventHander(item);
            }
            if (text != "OK")
            {
                XtraMessageBox.Show(text, "Cảnh B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            this.DoHide();
            return text;
        }

        private void xucSymbolAdd_Load(object sender, EventArgs e)
        {
        }
    }
}